/*
 * @lc app=leetcode.cn id=69 lang=cpp
 *
 * [69] x 的平方根 
 */

// @lc code=start
class Solution {
public:
    int mySqrt(int x) {
    //二分法
        int l = 0, r = x, ans = -1;
    
        while(l <= r) {
            int mid = l + (r - l)/2;
            if((long long)mid * mid <= x) {
                ans = mid;
                l = mid + 1;
            }else {
                r = mid - 1;
            }
        }  
        return ans;
    }
};
// @lc code=end

// int ans = 0;
//         for(long long i = 0;i < 65532;i++) {
//             if(i*i > x) {
//                 ans = i-1;
//                 break;
//             }
//         }
//         return ans;